package com.moter.estest.service.impl;

import com.moter.estest.entity.Doc;
import com.moter.estest.entity.DocFromInfo;
import com.moter.estest.service.DocService;
import com.moter.estest.util.Base64Utils;
import com.moter.estest.util.ESProperties;
import com.moter.estest.util.ESUtils;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class DocServiceImpl implements DocService {
    @Autowired
    private ESUtils<Doc> esUtils;

    @Autowired
    private ESProperties esProperties;
    @Override
    public boolean uploadFile(InputStream inputStream,String originalFileName) throws IOException {
        String data = Base64Utils.encodeBase64File(inputStream);
        Doc doc = new Doc().setTitle(originalFileName).setData(data);
        return  esUtils.addDoc(esProperties.getIndex(), null, doc, esProperties.getPipeline());
    }

    @Override
    public List<DocFromInfo> searchDocFromInfosByKeyword(int pageNo,int pageSize,String keyword) throws IOException {
        SearchResponse searchResponse = esUtils.searchContentHighlight(esProperties.getIndex(), pageNo, pageSize, keyword);
        //获取所有匹配
        SearchHits hits = searchResponse.getHits();
        ArrayList<DocFromInfo> docFromInfos = new ArrayList<>();
        for (SearchHit hit : hits) {
            DocFromInfo docFromInfo = new DocFromInfo().setId(hit.getId());
            //获取所有高亮
            Map<String, HighlightField> highlightFields = hit.getHighlightFields();
            //获取标题
            HighlightField titleHighlight = highlightFields.get("title");
            if(titleHighlight!=null) {
                String title = titleHighlight.getFragments()[0].toString();
                docFromInfo.setTitle(title);
            }else{
                docFromInfo.setTitle(hit.getSourceAsMap().get("title").toString());
            }

            //获取attachment.content
            HighlightField attachmentContent = highlightFields.get("attachment.content");
            for (Text fragment : attachmentContent.getFragments()) {
                // 去掉多余空格
                String string = fragment.toString().replace("\n\n", "\n").replace("\n\n", "\n").replace("\n\n", "\n");
                if(string.contains("<em>处</em><em>罚</em><em>机</em><em>构</em>".replace("<em>","<span style='color:red;'>").replace("</em>","</span>"))) {
                    docFromInfo.setFourEle(string);
                }else {
                    docFromInfo.setBody(string);
                }
            }
            docFromInfos.add(docFromInfo);
        }
        return docFromInfos;

    }
}
